package com.example.yogurtsystem.exception;

import com.example.yogurtsystem.dto.ResultDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.stream.Collectors;

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {


    private final Logger logger = LoggerFactory.getLogger(getClass());

    @ExceptionHandler(ConstraintViolationException.class)
    public ResultDTO<?> constraintViolationException(ConstraintViolationException ex, WebRequest request) {
        logger.warn("constraintException : [ {} ]", ex.toString());
        return ResultDTO.response(ex.getMessage());
    }

    @ExceptionHandler(DataIntegrityViolationException.class)
    public ResultDTO<?> dataIntegrityViolationException(DataIntegrityViolationException ex, WebRequest request) {
        logger.warn("dataIntegrityException :[ {} ]", ex.toString());
        return ResultDTO.response(ex.getMessage());
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResultDTO<?> methodNotSupportedException(HttpRequestMethodNotSupportedException ex, WebRequest request) {

        logger.warn("methodNotSupportedException : [ {} ]", ex.toString());
        return ResultDTO.response(ex.getMessage());
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ResultDTO<?> maxUploadSizeExceededException(MaxUploadSizeExceededException ex, WebRequest request) {

        logger.warn("maxUploadSizeExceededException : [ {} ]", ex.toString());
        return ResultDTO.response(ex.getMessage());
    }

    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResultDTO<?> httpMessageNotReadableException(HttpMessageNotReadableException ex, WebRequest request) {

        logger.error("HttpMessageNotReadableException : [ {} ]", ex.toString());
        return ResultDTO.response(ex.getMessage());
    }

    // 数据校验错误 POST请求时抛出的异常
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResultDTO<?> methodArgumentNotValidExceptionCatch(MethodArgumentNotValidException e) {
        StringBuilder sb = new StringBuilder();
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        String message = allErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining(";"));
        logger.warn("MethodArgumentNotValidException : [ {} ]", message);
        return ResultDTO.response(message);
    }

    @ExceptionHandler(value = ServiceException.class)
    public ResultDTO<?> serviceException(ServiceException ex, WebRequest request) {
        logger.warn("ServiceException : [ {} ]", ex.toString());
        return ResultDTO.response(ex.getMessage());
    }
}